var Camerizer = Camerizer||{};

Camerizer._camerize = function (value, offset) {
	if (value == null) return null;
	if (value == "") return "";
	var str = value.replace(/^_+|_+$/, "");
	var ret = str.substring(0, offset).toUpperCase();
	for (var i=offset, l=str.length; i<l; i++) {
		var c = str.substring(i, i+1);
		if (c == "_") {
			ret += str.substring(++i, i+1).toUpperCase();
		} else {
			ret += c.toLowerCase();
		}
	}
	return ret;
}

Camerizer.camerize = function (value, isLower) {
	if (isLower) return Camerizer._camerize(value, 0);
	return Camerizer._camerize(value, 1);
}

Camerizer.lowerCamerize = function (value) {
	return Camerizer.camerize(value, true);
}

Camerizer.upperCamerize = function (value) {
	return Camerizer.camerize(value, false);
}

Camerizer.reverseCamerize = function (value, isLower) {
	if (value == null) return null;
	if (value == "") return "";
	if (value.indexOf("_") >= 0) {
		var message = value + " => アンダースコアが含まれるため変換できません。";
		throw {
			message : message, description : message,
			number : -1, code : -1,
			toString : function(){return this.message}
		}
	}
	var ret = value.substring(0, 1).toLowerCase();
	for (var i=1, l=value.length; i<l; i++) {
		var c = value.substring(i, i+1);
		if (c >= "A" && c <= "Z") {
			ret += "_" + c.toLowerCase();
		} else {
			ret += c;
		}
	}
	if (isLower) return ret;
	return ret.toUpperCase();
}
